// TimeData Comparer - Comparer for sorting TimeData objects by Signature.
// Copyright (C) 2009  Robert N. Tenney
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;


namespace RTenney.Utility.Timekeeping
{
    /// <summary>
    /// Description of TimeDataSignatureComparer.
    /// </summary>
    public class TimeDataSignatureComparer : IComparer<TimeData>
    {
        /// <summary>
        /// Compares the Signature of one TimeData object with another TimeData object.
        /// </summary>
        /// <param name="x">First TimeData object.</param>
        /// <param name="y">Second TimeData object.</param>
        /// <returns></returns>
        public int Compare(TimeData x, TimeData y)
        {
            if (x.Signature == null)
            {
                if (y.Signature == null)
                {
                    // If x is null and y is null, they're
                    // equal.
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater.
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y.Signature == null)
                    // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    //                    // ...and y is not null, compare the
                    //                    // content of the two strings.
                    //                    //
                    //                    int retval = x.Signature.Length.CompareTo(y.Signature.Length);
//
                    //                    if (retval != 0)
                    //                    {
                    //                        // If the strings are not of equal length,
                    //                        // the longer string is greater.
                    //                        //
                    //                        return retval;
                    //                    }
                    //                    else
                    //                    {
                    // If the strings are of equal length,
                    // sort them with ordinary string comparison.
                    //
                    return x.Signature.CompareTo(y.Signature);
                }
                //                }
            }
        }
    }
}

